home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / MEMMON_F / MBATCH.C < prev    next >
Text File  |  1990-03-02  |  3KB  |  157 lines

  1. /*
  2.  * mbatch.c: basic interface for batch-mode output.
  3.  */
  4.  
  5. #include "memmon.h"
  6.  
  7. static int nframes = 0;            /* number of images written */
  8.  
  9. /*
  10.  *  Text buffer.
  11.  */
  12. static char tbuf[TextLines][TextLength + 1];        /* text chars */
  13. static unsigned char tfg[TextLines][TextLength + 1];    /* foreground color */
  14. static unsigned char tbg[TextLines][TextLength + 1];    /* background color */
  15.  
  16. /*
  17.  *  Memory map buffer.
  18.  */
  19.  
  20. static unsigned char *mbuf;        /* pixel color buffer */
  21. static word mbufsiz = 0;        /* current allocated size */
  22.  
  23. /*
  24.  * devmap() - load color map into device.
  25.  */
  26.  
  27. novalue devmap()
  28.    {
  29.    /* nothing to do */
  30.    }
  31.  
  32. /*
  33.  * devflood(c) - fill image with color c.
  34.  */
  35.  
  36. novalue devflood(c)
  37. int c;
  38.    {
  39.    word n;
  40.  
  41.    n = (word)memheight * (word)width;    /* max display w/o new refresh */
  42.    if (n > mbufsiz) {
  43.       if (mbuf)
  44.          free((char *)mbuf);
  45.       n = (word)memheight * (word)width;
  46.       mbuf = (unsigned char *)malloc((msize)n);
  47.       if (!mbuf) {
  48.          fprintf(stderr, "%s: out of memory", progname);
  49.          exit(ErrorExit);
  50.          }
  51.       mbufsiz = n;
  52.       }
  53.  
  54.    memfill((char *)mbuf, c, (word)mbufsiz);
  55.    memfill((char *)tbuf, 0, (word)(TextLines * TextLength));
  56.    memfill((char *)tbg, c, (word)(TextLines * TextLength));
  57.    }
  58.  
  59. /*
  60.  * devpaint(start, n, color, b) - paint n pixels in given color.
  61.  *  If b >= 0, the last pixel is to be that color instead (for a border)
  62.  */
  63.  
  64. novalue devpaint(s, n, c, b)
  65. word s, n;
  66. int c, b;
  67.    {
  68.    unsigned char *p;
  69.  
  70.    if (b >= 0)                /* if border, decr total count */
  71.       n--;
  72.    p = mbuf + s;
  73.    while (n--)                /* fill pixels */
  74.       *p++ = c;
  75.    if (b >= 0)                /* if border, set its value */
  76.       *p++ = b;
  77.    }
  78.  
  79. /*
  80.  * devtext(string, row, col, fgcolr, bgcolr) - write text data.
  81.  */
  82.  
  83. novalue devtext(s, row, col, fg, bg)
  84. char *s;
  85. int row, col, fg, bg;
  86.    {
  87.    int n;
  88.  
  89.    n = strlen(s) + 1;
  90.    while (n--)  {            /* copy including terminator */
  91.       tbuf[row][col] = *s++;
  92.       tfg[row][col] = fg;
  93.       tbg[row][col] = bg;
  94.       if (col++ > TextLength)
  95.          break;                /* but no wraparound */
  96.       }
  97.    }
  98.  
  99. /*
  100.  * devsnap() - take a snapshot of the current display.
  101.  */
  102. novalue devsnap()
  103.    {
  104.    int c, row, col, k;
  105.  
  106.    batbegin();                /* begin batch frame */
  107.  
  108.    /*
  109.     * identify text strings and pass individually
  110.     */
  111.    if (textrow > 0) {
  112.       for (row = 0; row < TextLines; row++)  {    /* for each text line */
  113.          col = 0;
  114.          while (col < TextLength)  {
  115.             if (tbuf[row][col] == '\0' || tfg[row][col] == tbg[row][col])
  116.                col++;            /* skip unused text positions */
  117.             else {
  118.                for (k = col;
  119.                   k < TextLength && tfg[row][k]==tfg[row][col] && tbuf[row][k];
  120.                   k++)
  121.                      ;            /* find run of one color */
  122.                c = tbuf[row][k];
  123.                tbuf[row][k] = '\0';    /* temporarily terminate string */
  124.                battext(tbuf[row] + col, row, col,
  125.                   (int)tfg[row][col], (int)tbg[row][col]);
  126.                tbuf[row][k] = c;    /* restore text buffer */
  127.                col = k;
  128.                }
  129.             }
  130.          }
  131.       }
  132.  
  133.    /*
  134.     * dump memory to finish up
  135.     */
  136.    batmem(mbuf);            /* write memory dump, finish frame */
  137.    nframes++;
  138.    }
  139.  
  140. /*
  141.  * devflush() - flush output.  (No action needed.)
  142.  */
  143.  
  144. novalue devflush()
  145.    {
  146.    }
  147.  
  148. /*
  149.  * devterm() - terminate graphics.
  150.  */
  151.  
  152. novalue devterm()
  153.    {
  154.    fprintf(stderr, "%d %s written\n", nframes, (nframes==1)?"frame":"frames");
  155.    batterm();
  156.    }
  157.